package com.cmol.peopledetectapp.admin;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;
import com.cmol.peopledetectapp.json.AdminLog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 系统日志列表
 *
 * @author 张瀚文 邢文成 王国良
 */
public class ListLogActivity extends AppCompatActivity {

    private List<BeanLog> data = new ArrayList<BeanLog>();
    private Network network;
    private List<AdminLog> adminLogList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_log);

        /**
         * 初始化页面内容
         */
        Init();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /**
         * 获取 RecyclerView 对象
         */
        RecyclerView recyclerView = findViewById(R.id.RecycleView);

        /**
         * 自定义并设置线性布局
         */
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        /**
         * 声明 Adapter 辅助类
         */
        MyAdapterLog myAdapter = new MyAdapterLog(data, this);
        recyclerView.setAdapter(myAdapter);

        /**
         * 列表点击事件（保留）
         */
        myAdapter.setRecyclerItemClickListener(new MyAdapterLog.onRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                // 点击事件
            }
        });
    }

    /**
     * 页面初始化函数
     */
    private void Init() {
        /**
         * 请求报文体json
         */
        JSONObject jsonObject = new JSONObject();
        network = new Network(jsonObject, "/admin/viewAdminLog") {

            @Override
            protected void process(Call call, Response response) throws IOException {
                try {
                    final String body = Objects.requireNonNull(response.body()).string().trim();
                    JSONObject res = new JSONObject(body);
                    String array = res.getString("adminLogList");
                    Gson gson = new Gson();
                    Type adminLogeListType = new TypeToken<ArrayList<AdminLog>>() {
                    }.getType();
                    adminLogList = gson.fromJson(array, adminLogeListType);
                    for (AdminLog adminLog : adminLogList) {
                        BeanLog beanLog = new BeanLog();
                        beanLog.setName(adminLog.getId().toString());
                        beanLog.setOperation(adminLog.getOperation().toString());
                        data.add(beanLog);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void failureWarn() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListLogActivity.this, "网络请求异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }
}